﻿package org.flixel.system
{
    import org.flixel.*;
    import org.flixel.system.replay.*;

    public class FlxReplay extends Object
    {
        public var seed:Number;
        public var frame:int;
        public var frameCount:int;
        public var finished:Boolean;
        protected var _frames:Array;
        protected var _capacity:int;
        protected var _marker:int;

        public function FlxReplay()
        {
            this.seed = 0;
            this.frame = 0;
            this.frameCount = 0;
            this.finished = false;
            this._frames = null;
            this._capacity = 0;
            this._marker = 0;
            return;
        }// end function

        public function destroy() : void
        {
            if (this._frames == null)
            {
                return;
            }
            var _loc_1:* = this.frameCount - 1;
            while (_loc_1 >= 0)
            {
                
                (this._frames[_loc_1--] as FrameRecord).destroy();
            }
            this._frames = null;
            return;
        }// end function

        public function create(param1:Number) : void
        {
            this.destroy();
            this.init();
            this.seed = param1;
            this.rewind();
            return;
        }// end function

        public function load(param1:String) : void
        {
            var _loc_3:String = null;
            this.init();
            var _loc_2:* = param1.split("\n");
            this.seed = Number(_loc_2[0]);
            var _loc_4:uint = 1;
            var _loc_5:* = _loc_2.length;
            while (_loc_4 < _loc_5)
            {
                
                _loc_3 = _loc_2[_loc_4++] as String;
                if (_loc_3.length > 3)
                {
                    var _loc_7:String = this;
                    _loc_7.frameCount = this.frameCount + 1;
                    this._frames[++this.frameCount] = new FrameRecord().load(_loc_3);
                    if (this.frameCount >= this._capacity)
                    {
                        this._capacity = this._capacity * 2;
                        this._frames.length = this._capacity;
                    }
                }
            }
            this.rewind();
            return;
        }// end function

        protected function init() : void
        {
            this._capacity = 100;
            this._frames = new Array(this._capacity);
            this.frameCount = 0;
            return;
        }// end function

        public function save() : String
        {
            if (this.frameCount <= 0)
            {
                return null;
            }
            var _loc_1:* = this.seed + "\n";
            var _loc_2:uint = 0;
            while (_loc_2 < this.frameCount)
            {
                
                _loc_1 = _loc_1 + (this._frames[_loc_2++].save() + "\n");
            }
            return _loc_1;
        }// end function

        public function recordFrame() : void
        {
            var _loc_1:* = FlxG.keys.record();
            var _loc_2:* = FlxG.mouse.record();
            if (_loc_1 == null && _loc_2 == null)
            {
                var _loc_3:String = this;
                var _loc_4:* = this.frame + 1;
                _loc_3.frame = _loc_4;
                return;
            }
            var _loc_4:String = this;
            _loc_4.frameCount = this.frameCount + 1;
            var _loc_3:* = this.frameCount + 1;
            var _loc_4:String = this;
            _loc_4.frame = this.frame + 1;
            this._frames[_loc_3] = new FrameRecord().create(this.frame++, _loc_1, _loc_2);
            if (this.frameCount >= this._capacity)
            {
                this._capacity = this._capacity * 2;
                this._frames.length = this._capacity;
            }
            return;
        }// end function

        public function playNextFrame() : void
        {
            FlxG.resetInput();
            if (this._marker >= this.frameCount)
            {
                this.finished = true;
                return;
            }
            var _loc_2:String = this;
            _loc_2.frame = this.frame + 1;
            if ((this._frames[this._marker] as FrameRecord).frame != this.frame++)
            {
                return;
            }
            var _loc_2:String = this;
            _loc_2._marker = this._marker + 1;
            var _loc_1:* = this._frames[this._marker++];
            if (_loc_1.keys != null)
            {
                FlxG.keys.playback(_loc_1.keys);
            }
            if (_loc_1.mouse != null)
            {
                FlxG.mouse.playback(_loc_1.mouse);
            }
            return;
        }// end function

        public function rewind() : void
        {
            this._marker = 0;
            this.frame = 0;
            this.finished = false;
            return;
        }// end function

    }
}
